/* Author: vuanhkhai */
(function( $ ){
    $.fn.autoscroll = function(options) {
        /* init function */
         var settings = $.extend( {
                   list : '.list',
                    item : '.item',
                    duration: 5000,
                    vertical : false
              }, options);

        /* Select 'content', 'list', and 'item' objects */
        var content = jQuery(this);
         var list = content.find(settings.list);
         var items = list.find(settings.item);
         if( items.length == 0 ) { return; }
         
         var contentSize = null;
         var itemSize = null;
         var gap = null;
         if (settings.vertical) {
              contentSize = content.height();
              itemSize = items.height();
              gap = parseInt(items.css('margin-top'));
         } else {
              contentSize = content.width();
              itemSize = items.width();
              gap = parseInt(items.css('margin-left'));
         }

         /* Remove all placeholder in list. Note that 'placeholder' jQuery
          * object still exists. The 'remove' method do not remove jQuery
          * objects but HTML objects. 
          */
        list.find(settings.item).remove();
         
         var i = 0;
        function appendItem() {
            /* Append a 'item' HTML object to the list. */
            var item = items.get(i);
            list.append(items.get(i).outerHTML);

            /* reset 'list' object */
              list = content.find(settings.list);

            /* Add mouse events to items. */
              list.find(settings.item).unbind().mouseover(function() {
                  list.stop(); /* Stop animating */
             }).mouseout(function() {
                  scroll(); /* Restart animating */
             });

             /* keep 'i' is always from 0 to items.size() */
            i = (i + 1) % items.size();
        };

        /* Fill list by items so that no blank is in list. */
         do {
              appendItem();
              var n = list.find(settings.item).size();
             var size = n * itemSize + (n - 1) * gap;
             if (settings.vertical) {
                 list.height(size);
             } else {
                  list.width(size);
             }
         } while((settings.vertical ? list.height(): list.width()) < 2*contentSize);

         /* Animating function */
         function scroll() {
              var step = itemSize + gap;
              var position = list.position();
              var duration = settings.duration;
              
             if ((!settings.vertical && position.left < 0) ||
                       (settings.vertical && position.top < 0)) {
                  if (settings.vertical) {
                       step += position.top;
                  } else {
                       step += position.left;
                  }
                  duration *= step / (itemSize + gap); 
             }
             
             var attr = settings.vertical ? {top: '-=' + step} : {left: '-=' + step};
             var direction = settings.vertical ? 'top' : 'left';
              list.animate(attr,
                        duration,
                    'linear',
                    function() {
                      list.find(settings.item + ':first-child').remove();
                      list.css(direction, 0);
                       appendItem();
                            
                        scroll();
                    }
               );
         };

         /* Start animating */
         scroll();
    };
})( jQuery );
